//
//  NewfeatureCollectionViewController.swift
//  weibosina
//
//  Created by hwm on 16/8/28.
//  Copyright © 2016年 hwm. All rights reserved.
//

import UIKit

private let reuseIdentifier = "newfeatureCell"

class NewfeatureCollectionViewController: UICollectionViewController {

    // 页面个数
    private let pageCount: Int = 4
    // 布局对象
    private var layout: UICollectionViewFlowLayout = NewfeatureFlow()
    
    // 因为系统指定的初始化构造方法是带参数的(collectionViewLayout), 而不是不带参数的, 所以不用写override
    init() {
        super.init(collectionViewLayout: layout)
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    override func viewDidLoad() {
        super.viewDidLoad()
        // 1.注册一个cell
        self.collectionView!.registerClass(NewfeatureCell.self, forCellWithReuseIdentifier: reuseIdentifier)
    }

    // MARK: UICollectionViewDataSource
//    override func numberOfSectionsInCollectionView(collectionView: UICollectionView) -> Int {
//        return pageCount
//    }
    override func collectionView(collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        return pageCount
    }

    override func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell {
        
        // 1.获取cell
        let cell = collectionView.dequeueReusableCellWithReuseIdentifier(reuseIdentifier, forIndexPath: indexPath) as! NewfeatureCell
        // 2.设置cell的数据
        cell.imageindex = indexPath.item
        // 3.返回cell
        return cell
    }

    // MARK: UICollectionViewDelegate
    override func collectionView(collectionView: UICollectionView, didEndDisplayingCell cell: UICollectionViewCell, forItemAtIndexPath indexPath: NSIndexPath) {
        // 传递给我们的是上一页的索引
        //        print(indexPath)
        
        // 1.拿到当前显示的cell对应的索引
        let path = collectionView.indexPathsForVisibleItems().last!
        print(path)
        if path.item == (pageCount - 1) {
            // 2.拿到当前索引对应的cell
            let cell = collectionView.cellForItemAtIndexPath(path) as! NewfeatureCell
            // 3.让cell执行动画
            cell.starBtnAnimation()
        }
        
    }

}

// MARK: 自定义cell
private class  NewfeatureCell: UICollectionViewCell  {
    
    // MARK: - 懒加载
    private lazy var iconView = UIImageView()
    private lazy var startButton: UIButton = {
        let btn = UIButton.init(type: UIButtonType.Custom)
        btn.setBackgroundImage(UIImage(named: "new_feature_button"), forState: UIControlState.Normal)
        btn.setBackgroundImage(UIImage(named: "new_feature_button_highlighted"), forState: UIControlState.Highlighted)
        btn.hidden = true
        btn.addTarget(self, action: #selector(NewfeatureCell.customBtnClick), forControlEvents: UIControlEvents.TouchUpInside)
        return btn
    }()
    @objc func customBtnClick() {
        // 去主页, 注意点: 企业开发中如果要切换根控制器, 最好都在appdelegate中切换
        NSNotificationCenter.defaultCenter().postNotificationName(WMSwitchRootViewControllerKey, object: true)
    }
    // 保存图片的索引
    // Swift中被private休息的东西, 如果是在同一个文件中是可以访问的
    private var imageindex: Int? {
        didSet {
            iconView.image = UIImage.init(named: "new_feature_\(imageindex! + 1)")
        }
    }
    
    /**
     让按钮做动画
     */
    func starBtnAnimation() {
        weak var weakSelf = self
        startButton.hidden = false
        startButton.userInteractionEnabled = false
        startButton.transform = CGAffineTransformMakeScale(0.0, 0.0)
        UIView.animateWithDuration(2.0, delay: 0, usingSpringWithDamping: 0.8, initialSpringVelocity: 10, options: UIViewAnimationOptions.init(rawValue: 0), animations: {
            // 清空形变
            weakSelf!.startButton.transform = CGAffineTransformIdentity
            }) { (true) in
                weakSelf!.startButton.userInteractionEnabled = true
        }
    }
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        // 1.初始化UI
        setUI()
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    private func setUI() {
        
        // 1.添加子控件到contentView上
        addSubview(iconView)
        addSubview(startButton)
        
        // 2.设置子子控件的位置
        iconView.wm_Fill(contentView)
        startButton.wm_AlignInner(type: wm_AlignType.BottomCenter, referView: contentView, size:nil, offset: CGPoint(x: 0, y: -160))
    }
    
    
}


// MARK: 自定义布局
private class NewfeatureFlow: UICollectionViewFlowLayout {
    
    // 准备布局
    // 1.先调用一个有多少行cell 2.调用准备布局 3.调用返回cell
    override func prepareLayout() {
        super.prepareLayout()
        
        // 1.设置layout布局
        itemSize = CGSizeMake(screenWidth, screenHeight)
        minimumInteritemSpacing = 0
        minimumLineSpacing = 0
        // 设置滑动方向
        scrollDirection = UICollectionViewScrollDirection.Horizontal
        
        // 2.设置collectionView的属性
        collectionView?.pagingEnabled = true
        collectionView?.showsVerticalScrollIndicator = false
        collectionView?.showsHorizontalScrollIndicator = false
        collectionView?.bounces = false
    }
    
}





